/*
 *
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','poolList');

/*controller部分*/

//主要操作controller
authApp.controller("tableFiveController",function($scope,$http,$getQueryString,$timeout,$sce,$cookies){
	$scope.accounttype = $cookies.get('accounttype')
    $scope.schoolId=$getQueryString("schoolId");
    $scope.schoolName=decodeURIComponent($getQueryString("schoolName"));
    var accounttype = $cookies.get('accounttype')
    $scope.isSecond="";
    $scope.title = accounttype == 'material' ? '材料评审专家组投票汇总表[机密]':'现场考察专家组投票汇总表[机密]'
    $scope.expectType = accounttype == 'material' ? 0 : 1
    /*try{
        $scope.isSecond=$getQueryString("isSecond");
        var li = document.getElementById("material_manage");
        li.setAttribute("class", "choosen");
    }catch(msg){}*/
    //$scope.backTarget="opinion_record.html";
    //**********主体方法
    $scope.ifFirstTimeQuery=true;
    $scope.isEdit=true;
    $scope.exTitleInfo="分项意见记录表";
    if($scope.state=="0"){
        $scope.titleInfo="填写";
    }else if($scope.state=="1"){
        $scope.titleInfo="修改";
    }else if($scope.state=="2"){
        //$scope.exTitleInfo="分项意见记录表列表";
        $scope.titleInfo="查看";
        $scope.isEdit=false;
        //$scope.backTarget="opinion_summary.html";
    }

    var queryMainApi=apis.queryPool;
    var requestJson={'schoolId':$scope.schoolId, 'expectType': $scope.expectType};
    $scope.queryMain=function(){
        $http({
            method:'POST',
            url:queryMainApi,
            data:requestJson,
        }).success(function(response){
            if(response.code=="000000"){
                $scope.queryMainApiCallBack(response);
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("网络出现故障，请确认网络是否连接");
        })
    }
    if(undefined==$scope.isSecond){
        var btarget=$scope.backTarget.split(".");
        var li = document.getElementById(btarget[0]);
        li.setAttribute("class", "choosen");
    }
    $scope.queryMain();

    /*下面为自已编辑部分*/

    //调用主接口成功之后的处理方法
    $scope.queryMainApiCallBack=function(response){
        if(response.code=="000000"){
            //$scope.list=response.data;
            $scope.chk = function(num){
                return num?num%2?"奇数":"偶数":"0"
            }
            $scope.boolenValue = $scope.chk(response.data.length)=='偶数';

            $scope.gradeA=0;
            $scope.gradeB=0;
            $scope.gradeC=0;

            var Arr=[];
            for( var i=0;i<response.data.length;i=i+2){
                var newArr=[];
                var newJson={};
                newJson.name=response.data[i].title;
                newJson.key=response.data[i]?response.data[i].s_key:'';
                newJson.value=response.data[i]?response.data[i].s_key:'';
                newJson.title=response.data[i]?response.data[i].s_value:'';
                if(response.data[i]){
                    if(response.data[i].s_key=='A')
                        $scope.gradeA++;
                    if(response.data[i].s_key=='B')
                        $scope.gradeB++;
                    if(response.data[i].s_key=='C')
                        $scope.gradeC++;
                }
                newArr.push(newJson);
                if((i+1)<response.data.length){
                    var newJson2={};
                    newJson2.name=response.data[i+1].title;
                    newJson2.key=response.data[i+1]?response.data[i+1].s_key:'';
                    newJson2.value=response.data[i+1]?response.data[i+1].s_key:'';
                    newJson2.title=response.data[i+1]?response.data[i+1].s_value:'';
                    if(response.data[i+1]){
                        if(response.data[i+1].s_key=='A')
                            $scope.gradeA++;
                        if(response.data[i+1].s_key=='B')
                            $scope.gradeB++;
                        if(response.data[i+1].s_key=='C')
                            $scope.gradeC++;
                    }
                    newArr.push(newJson2);
                }
                Arr.push(newArr);
            }
            console.log(Arr);
            $scope.list=Arr;
            /* angular.forEach($scope.list,function(data,index,array){
             if(data){
             data.num=$scope.number+"-"+data.number;
             var t=data.number;
             angular.forEach(data.list,function(data,index,array){
             data.num=$scope.number+"."+t+"."+data.number;
             })
             }
             })*/
        }else{
            alert(response.errorMsg);
        }
    }

    
    /*
     *
     * 其它操作方法
     *
     */

    //根据细则值计算二级指标等级
    $scope.showResult=function(){
        angular.forEach($scope.list,function(data,index,array){
            var anum=0;
            var bnum=0;
            var cnum=0;
            var count=0;//细则空值数量
            angular.forEach(data.list,function(data,index,array){
                switch(data.level){
                    case"A":anum++;
                        break;
                    case"B":bnum++;
                        break;
                    case"C":cnum++;
                        break;
                    default:count++;
                }
            })
            if(data.list.length>0){
                if(count>0)
                    data.level="";
                else{
                    if(bnum==0 && cnum==0)
                        data.level="A";
                    else if(bnum>0 && cnum==0)
                        data.level="B";
                    else if(cnum>0)
                        data.level="C";
                }
            }
        })
    }

    //表格数据重置
    $scope.resetAll=function(){
        if(window.confirm("是否确认重置数据？")){
            angular.forEach($scope.list,function(data,index,array){
                angular.forEach(data.list,function(data,index,array){
                    data.level="";
                })
                $scope.showResult();
            })
        }
    }

    //返回到前页面
    $scope.goback=function(){
        javascript:history.back(-1);
    }

    //表格数据保存
    $scope.saveAll=function(){
        if(window.confirm("是否确认保存数据？")){
            var isAllow=true;
            var detailLevel="";
            var twoLevel="";
            var mainProblem="";

            angular.forEach($scope.list,function(data,index,array){
                if(data.level==""||(data.level!="A"&&data.mainProblem==""))
                    isAllow=false;
                else{
                    twoLevel+=data.level+"#";
                    if(data.mainProblem)
                        mainProblem+=data.mainProblem+"#";
                    else
                        mainProblem+=" #";
                    angular.forEach(data.list,function(data,index,array){
                        detailLevel+=data.level+",";
                    })
                    detailLevel+="#";
                }
            })

            if(isAllow){
                var data={'schoolId':$scope.schoolId,'itemId':$scope.itemId,'detailLevel':detailLevel,'twoLevel':twoLevel,'mainProblem':mainProblem};
                $http({
                    url:apis.saveAuthExpertItemData,
                    method:"post",
                    data:data,
                }).success(function(response){
                    if(response.code=="000000"){
                        alert("保存数据成功！");
                        location.href="opinion_record.html";
                    }
                })
            }else{
                alert("信息未填充完整");
            }
        }
    }

    //表格数据提交
    $scope.submitAll=function(){
        if(window.confirm("是否确认提交数据？提交后将无法修改！")){
            var isAllow=true;
            var detailLevel="";
            var twoLevel="";
            var mainProblem="";

            angular.forEach($scope.list,function(data,index,array){
                if(data.level==""||(data.level!="A"&&data.mainProblem==""))
                    isAllow=false;
                else{
                    twoLevel+=data.level+"#";
                    if(data.mainProblem)
                        mainProblem+=data.mainProblem+"#";
                    else
                        mainProblem+=" #";
                    angular.forEach(data.list,function(data,index,array){
                        detailLevel+=data.level+",";
                    })
                    detailLevel+="#";
                }
            })

            if(isAllow){
                var data={'schoolId':$scope.schoolId,'itemId':$scope.itemId,'detailLevel':detailLevel,'twoLevel':twoLevel,'mainProblem':mainProblem};
                $http({
                    url:apis.saveAuthExpertItemDataTable,
                    method:"post",
                    data:data,
                }).success(function(response){
                    if(response.code=="000000"){
                        alert("提交数据成功！");
                        location.href="opinion_record.html";
                    }
                })
            }else{
                alert("信息未填充完整");
            }
        }
    }

    $scope.download = function(){
        //$("#download_btn").unbind("click").bind("click" , exportExcel ) ;
        //function exportExcel(){
           return window.open('/views/investiga/table7.html?schoolId='+ $scope.schoolId+'&schoolName='+decodeURIComponent($getQueryString("schoolName")))
        //}
    
    }
})





